package cn.chrilwe.pigpig.im.service;
/**
 * 
 * @author chrilwe
 * 2020-7-26
 */

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cn.chrilwe.pigpig.core.base.Code;
import cn.chrilwe.pigpig.im.annotations.CheckLogin;
import cn.chrilwe.pigpig.im.model.ClientConnectDetail;
import cn.chrilwe.pigpig.im.model.TokenStore;
import cn.chrilwe.pigpig.im.protocol.TalkRequest;
import cn.chrilwe.pigpig.im.protocol.TalkResponse;
import cn.chrilwe.pigpig.im.service.util.Oauth2Util;

@Service
public class MessageService {
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	//发送消息
	@CheckLogin
	public TalkResponse topicMsg(TalkRequest talkRequest) {
		TalkResponse r = checkIsOnlineNotice(talkRequest.getToken());
		if(r != null) {
			return r;
		}
		String msg = talkRequest.getMsg();
		String msg1 = parseAndCheckMsg(msg);
		
		TalkResponse res = new TalkResponse();
		res.setStatus(Code.SUCCESS);
		String resMsg = "<div style=\"width:28px;height:20px;font-size:12px;background-color:#FF9933;text-align:center;display:inline;\">房管</div>&#32<img src=\"../../static/gongjue.png\" style=\"width:28px;height:25px;\"></img>&#32<div class=\"UserLevel\">50</div>&#32<div style=\"height:20px;color:#CC33FF;display:inline;\">xiaobai:</div>&#32<div style=\"height:20px;color:black;word-wrap:break-word;word-break:break-all;white-space:normal;display:inline;font-size:12px;\">"+msg1+"</div>";
		res.setResMsg(resMsg);
		return res;
	}
	
	//对消息进行解析，防止一些crsf攻击
	protected String parseAndCheckMsg(String msg) {
		String msg1 = msg.replaceAll("<", "&#60");
		String msg2 = msg1.replaceAll(">", "&#62");
		
		return msg2;
	}
	
	//校验是否为上线通知
	protected TalkResponse checkIsOnlineNotice(String token) {
		String ts = stringRedisTemplate.opsForValue().get(token);
		TokenStore tokenStore = JSON.parseObject(ts, TokenStore.class);
		
		ClientConnectDetail detail = parseToken(tokenStore);
		
		if(!StringUtils.isEmpty(ts)) {
			String s = stringRedisTemplate.opsForValue().get(detail.getUsername());
			if(StringUtils.isEmpty(s)) {
				saveClientDetail(detail);
				TalkResponse res = new TalkResponse();
				String resMsg = "<div style=\"width:28px;height:20px;font-size:12px;background-color:#FF9933;text-align:center;display:inline;\">房管</div>&#32<img src=\"../../static/gongjue.png\" style=\"width:28px;height:25px;\"></img>&#32<div class=\"UserLevel\">50</div>&#32<div style=\"height:20px;display:inline;\">xiaobai:</div>&#32<div style=\"height:20px;color:black;word-wrap:break-word;word-break:break-all;white-space:normal;display:inline;font-size:15px;color:red;\">欢迎来到直播间</div>";
				res.setResMsg(resMsg);
				res.setStatus(Code.SUCCESS);
				return res;
			}
		}
		return null;
	}
	
	//将jwt令牌解析
	protected ClientConnectDetail parseToken(TokenStore store) {
		
		return Oauth2Util.parse(store);
	}
	
	protected void saveClientDetail(ClientConnectDetail detail) {
		stringRedisTemplate.opsForValue().set(detail.getUsername(), JSON.toJSONString(detail));
	}
}
